package com.laolu.icon.utils;

import com.laolu.icon.constants.CSIDL;
import com.laolu.icon.exception.MyException;
import com.laolu.icon.win32.Shell32Extra;
import com.sun.jna.Memory;
import com.sun.jna.platform.win32.Kernel32;
import org.jetbrains.annotations.NotNull;

public class MyFolder {
	
	
	/**
	 * 获取当前用户桌面路径
	 * @return 桌面路径
	 * @throws MyException
	 */
	public String getUserDesktopPath() throws MyException {
		return getSpecialFolderPath(CSIDL.CSIDL_DESKTOP);
	}
	
	/**
	 * 获取公共桌面路径
	 * @return 公共桌面路径
	 * @throws MyException
	 */
	public String getPublicDesktopPath() throws MyException {
		return getSpecialFolderPath(CSIDL.CSIDL_COMMON_DESKTOPDIRECTORY);
	}
	
	/**
	 * 获取指定文件夹路径
	 * @param folderId
	 * @return 指定文件夹路径
	 * @throws MyException
	 */
	public String getSpecialFolderPath(@NotNull CSIDL folderId) throws MyException {
		Memory path = new Memory(260);
		boolean b = Shell32Extra.INSTANCE.SHGetSpecialFolderPath
				(null, path, folderId.getValue(), false);
		if (b) {
			return path.getWideString(0);
		}
		throw new MyException("MyFolder.getSpecialFolderPath failed error code : " + Kernel32.INSTANCE.GetLastError());
	}
}
